package com.wj.taobaoke;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class AnalysisWeb {
	
	
	public String getOneHtml(final String htmlurl, final String encodeType, boolean isValidateEncodeType){
		if(htmlurl == null || htmlurl.length() == 0){
			System.out.println("我靠htmlurl===null"+htmlurl);
			return "";
		}
		URL url;
		String temp;
		final StringBuilder sb = new StringBuilder();
		try {
			url = new URL(htmlurl);
			InputStream inputStream = url.openStream();
			ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(toByteArray(inputStream));
			BufferedReader in = null;
			if(isValidateEncodeType){
				in = changeBufferedReader(byteArrayInputStream, htmlurl);
			}else{
				in = new BufferedReader(new InputStreamReader(
						byteArrayInputStream, encodeType));// 读取网页全部内容
			}
			while ((temp = in.readLine()) != null) {
				sb.append(temp);
			}
			in.close();
		} catch (final MalformedURLException me) {
			System.out.println(me.getMessage());
		} catch (final Exception e) {
			System.out.println(e.getMessage());
		}
		return sb.toString();
	}
	
	public String getOneFile(final String fileName, final String encodeType){
		String temp;
		final StringBuilder sb = new StringBuilder();
		try {
			FileInputStream fileInputStream = new FileInputStream(new File("e:\\merchandisePromotion.htm"));
			ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(toByteArray(fileInputStream));
			BufferedReader in = new BufferedReader(new InputStreamReader(byteArrayInputStream, encodeType));// 读取网页全部内容
			while ((temp = in.readLine()) != null) {
				sb.append(temp);
			}
			in.close();
		} catch (final MalformedURLException me) {
			System.out.println(me.getMessage());
		} catch (final Exception e) {
			System.out.println(e.getMessage());
		}
		return sb.toString();
		
	}
	
	
	private BufferedReader changeBufferedReader(InputStream inputStream, String url) throws Exception{
		BufferedReader in = new BufferedReader(new InputStreamReader(
				inputStream, "gb2312"));// 读取网页全部内容
		String temp = null;
		String htmlCharset = null;
		while ((temp = in.readLine()) != null) {
			if(temp.trim().length() == 0){
				continue;
			}
			if(htmlCharset == null){
				String tempCharset = null;
				if((tempCharset = getForRegex(temp, "charset=\"(.*?)\"")).length() > 0){
					htmlCharset = tempCharset.replace("charset=", "").replace("\"", "");
					break;
				}
				if((tempCharset = getForRegex(temp, "charset=(.*?)\"")).length() > 0){
					htmlCharset = tempCharset.replace("charset=", "").replace("\"", "");
					break;
				}
			}
			if(temp.contains("</head>")){
				break;
			}
		}
		if(htmlCharset == null){
			System.out.println("我靠，htmlCharset == null, url="+url);
		}else{
			if(!"gb2312".equals(htmlCharset.toLowerCase())){
				inputStream.reset();
				in = new BufferedReader(new InputStreamReader(inputStream, htmlCharset.trim().toLowerCase()));// 读取网页全部内容
			}
		}
		return in;
	}
	
	private byte[] toByteArray(InputStream input) throws IOException {
	    ByteArrayOutputStream output = new ByteArrayOutputStream();
	    byte[] buffer = new byte[4096];
	    int n = 0;
	    while (-1 != (n = input.read(buffer))) {
	        output.write(buffer, 0, n);
	    }
	    return output.toByteArray();
	}
	
	public String getTitle(final String s) {
		String regex;
		String title = "";
		final List<String> list = new ArrayList<String>();
		regex = "<title>.*?</title>";
		final Pattern pa = Pattern.compile(regex, Pattern.CANON_EQ);
		final Matcher ma = pa.matcher(s);
		while (ma.find()) {
			list.add(ma.group());
		}
		for (int i = 0; i < list.size(); i++) {
			title = title + list.get(i);
		}
		return outTag(title);
	}

	public List<String> getLink(final String s) {
		String regex;
		final List<String> list = new ArrayList<String>();
		regex = "<a[^>]*href=(\"([^\"]*)\"|\'([^\']*)\'|([^\\s>]*))[^>]*>(.*?)</a>";
		final Pattern pa = Pattern.compile(regex, Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
		final Matcher ma = pa.matcher(s);
		while (ma.find()) {
			list.add(ma.group());
		}
		return list;
	}

	public List<String> getScript(final String s) {
		String regex;
		final List<String> list = new ArrayList<String>();
		regex = "<script.*?</script>";
		final Pattern pa = Pattern.compile(regex, Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
		final Matcher ma = pa.matcher(s);
		while (ma.find()) {
			list.add(ma.group());
		}
		return list;
	}

	public List<String> getCSS(final String s) {
		String regex;
		final List<String> list = new ArrayList<String>();
		regex = "<style.*?</style>";
		final Pattern pa = Pattern.compile(regex, Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
		final Matcher ma = pa.matcher(s);
		while (ma.find()) {
			list.add(ma.group());
		}
		return list;
	}
	
	public String outTag(final String s) {
		return s.replaceAll("<.*?>", "");
	}

	public String getForRegex(final String s, final String regex) {
		final StringBuffer sb = new StringBuffer();
		final Pattern pa = Pattern.compile(regex, Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
		final Matcher ma = pa.matcher(s);
		if (ma.find()) {
			sb.append(ma.group());
		}
		String temp = sb.toString();
		return temp;
	}
	
	public List<String> getContentListFroRegex(final String sourceStr, String regex) {
		final List<String> list = new ArrayList<String>();
		final Pattern pa = Pattern.compile(regex, Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
		final Matcher ma = pa.matcher(sourceStr);
		int counter = 0;
		while (ma.find()) {
			list.add(ma.group());
			counter++;
			if(counter > 100){
				break;
			}
		}
		return list;
	}	

}
